package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {

    @Autowired
    private AgencyCertificationAuditService agencyCertificationAuditService;

    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }


    /**
     * 机构提交认证申请
     * @param dto
     */
    @Override
    @Transactional
    public void agencyCertificationAudit(AgencyCertificationAuditAddReqDTO dto) {
        AgencyCertification agencyCertification = BeanUtil.copyProperties(dto, AgencyCertification.class);
        LambdaQueryWrapper<AgencyCertification> queryWrapper = Wrappers.<AgencyCertification>lambdaQuery().eq(AgencyCertification::getIdNumber, agencyCertification.getIdNumber());
        AgencyCertification one = this.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(one)){
            throw new CommonException("不能重复进行机构认证");
        }
        agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        boolean save = this.save(agencyCertification);
        if (!save){
            throw new DBException("操作失败");
        }
        AgencyCertificationAudit agencyCertificationAudit = new AgencyCertificationAudit();
        BeanUtil.copyProperties(dto, agencyCertificationAudit);
        agencyCertificationAudit.setServeProviderId(UserContext.currentUserId());
        agencyCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper1 = Wrappers.<AgencyCertificationAudit>lambdaQuery().eq(AgencyCertificationAudit::getIdNumber, dto.getIdNumber());
        AgencyCertificationAudit one1 = agencyCertificationAuditService.getOne(queryWrapper1);
        if (ObjectUtil.isNotEmpty(one1)){
            throw new CommonException("机构已存在");
        }
        boolean save1 = agencyCertificationAuditService.save(agencyCertificationAudit);
        if (!save1){
            throw new DBException("操作失败");
        }

    }

    /**
     * 查询最新的驳回原因
     * @return
     */
    @Override
    public RejectReasonResDTO rejectReason() {
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = Wrappers.<AgencyCertificationAudit>lambdaQuery().eq(AgencyCertificationAudit::getServeProviderId, UserContext.currentUserId()).orderByDesc(AgencyCertificationAudit::getCreateTime).last("limit 1");
        List<AgencyCertificationAudit> list = agencyCertificationAuditService.list(queryWrapper);
        AgencyCertificationAudit agencyCertificationAudit=null;
        for (AgencyCertificationAudit agencyCertificationAudit1 : list) {
            agencyCertificationAudit=agencyCertificationAudit1;
        }
        if (ObjectUtil.isEmpty(agencyCertificationAudit)){
            throw new CommonException("不存在");
        }
        return new RejectReasonResDTO(agencyCertificationAudit.getRejectReason());
    }


}
